package com.iseastar.guojiang.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.shouhuobao.station.R;

/**
 * 该类主要封装 app tab的切换效果
 * 
 * @author coffee <br>
 *         2016-1-15下午12:00:55
 */
public class AppTab implements View.OnClickListener {

	private final int TAB_SIZE = 2;
	private int currentIndex;
	private int moveLength;// 动画移动的距离
	private Activity context;

	private TextView[] mTabViews;
	private View mImgLine;

	private TabChangeListener changeListener;

	public interface TabChangeListener {
		public void onTabChanege(int which);
	}

	public AppTab(Activity context) {
		this.context = context;
	}

	/**
	 * @param viewPager
	 * @param line
	 * @param tags
	 */
	public void initPosition(View line, TextView[] tabs) {
		this.mImgLine = line;
		this.mTabViews = tabs;
		//
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mImgLine.getLayoutParams();
		params.leftMargin = 50;
		params.rightMargin = 50;
		params.width = screenW / 2 - params.leftMargin - params.rightMargin;
		mImgLine.setLayoutParams(params);
		moveLength = screenW / TAB_SIZE;
		//
		initStyle(0);
	}

	private void initStyle(int index) {
		for (int i = 0; i < mTabViews.length; i++) {
			mTabViews[i].setOnClickListener(this);
			mTabViews[i].setTypeface(null, Typeface.NORMAL);
			mTabViews[i].setTextColor(context.getResources().getColor(R.color.app_font_1));
		}
		currentIndex = index;
		mTabViews[currentIndex].setTypeface(null, Typeface.BOLD);
		mTabViews[currentIndex].setTextColor(context.getResources().getColor(R.color.app_theme_color));
	}

	public void setSelected(int index) {
		if (changeListener != null) {
			changeListener.onTabChanege(index);
		}
		// 要切换的是否当前
		boolean same = currentIndex == index;
		if (same == false) {
			initStyle(index);
			// 该方法需要在changeListener之后执行，因为currentIndex的值会被改变
			updateView();
		} else {
			// ignore
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		// 点击的是第一个tab
		if (id == mTabViews[0].getId()) {
			if (currentIndex == 0) {// 如果当前已经是tab == 0
				return;//
			} else {
				currentIndex = 0;
			}
		}
		// 点击的是第二个tab
		else {
			if (currentIndex == 1) {// 如果当前已经是tab == 1
				return;//
			} else {
				currentIndex = 1;
			}
		}
		if (changeListener != null) {
			changeListener.onTabChanege(currentIndex);
		}
		// 该方法需要在changeListener之后执行，因为currentIndex的值会被改变
		updateView();
	}

	private void updateView() {
		Animation animation = null;
		int preIndex;
		if (currentIndex == 1) {
			preIndex = 0;
			animation = new TranslateAnimation(0, moveLength, 0, 0);
		} else {
			preIndex = 1;
			animation = new TranslateAnimation(moveLength, 0, 0, 0);
		}
		mTabViews[currentIndex].setTextColor(context.getResources().getColor(R.color.app_theme_color));
		mTabViews[currentIndex].setTypeface(null, Typeface.BOLD);
		mTabViews[preIndex].setTextColor(context.getResources().getColor(R.color.app_font_1));
		mTabViews[preIndex].setTypeface(null, Typeface.NORMAL);
		// start animation
		animation.setFillAfter(true);
		animation.setDuration(300);
		mImgLine.startAnimation(animation);
	}

	public TabChangeListener getChangeListener() {
		return changeListener;
	}

	public void setChangeListener(TabChangeListener changeListener) {
		this.changeListener = changeListener;
	}
}
